package com.example.yunhailutong.zhongtan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.PeopleInfoEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by yunhailutong on 2018/4/26.
 */

public class CheckIdActivity extends ActivityBase {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.china_id)
    EditText china_id;
    @BindView(R.id.upload_idcard)
    Button uploadIdCard;

    PeopleInfoEntity peopleInfoEntity;
    private static  String idCard = null;
    @Override
    protected void addListener() {
        uploadIdCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idCard = china_id.getText().toString();
                if (idCard.isEmpty() || (idCard.length() != 15 && idCard.length() != 18)) {
                    disPlay("密码不能为空请重新输入");
                } else {
                    Map<String, String> params = new HashMap<>();
                    params.put("IdCard",idCard);
                    getPeopleInfo(params);
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_check_id;
    }

    public void getPeopleInfo(Map<String, String> params) {
        new PersionBiz().getPeopleInfo(params);
        showWaitingDialog();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Subscribe
    public void log(OldsEvent.GetPeopleInfoEvent msg) {

        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay("信息不正确，请联系管理员");
        }
        if (msg.what == 1) {
            peopleInfoEntity = (PeopleInfoEntity)msg.obj;
            if ( peopleInfoEntity != null
                    && peopleInfoEntity.getData() != null
                    && peopleInfoEntity.getData().get(0) != null
                    && peopleInfoEntity.getData().get(0).getRm_idCards() != null
                    && peopleInfoEntity.getData().get(0).getRm_idCards().equals(idCard)) {
                Intent intent = new Intent(this, ActivityRegister.class);
                startActivity(intent);
            } else {
                disPlay("信息不正确，请联系管理员");
            }

            finish();
        }
    }
}
